<?php
/**
 * @version : 1.0
 * @package : lib/struct
 * @name :  CLog
 * @todo:  Lớp  cơ sở CLog.  Xử lý Log.
*/
class CLog
{
	public $table;
	static $current = false;
	public $start_time;
	function __construct($table) {
		$this->start_time = isset($GLOBALS['start_time'])?$GLOBALS['start_time']:microtime(true);
		$this->table = $table;
	}

	function log($type, $message) {
		if(DEBUG) {
			$back_trace = DEBUG?output()->get(debug_print_backtrace()).'':'';
			$this->table->insert(array('type'=>$type, 'time'=>microtime(true), 'message'=>$message, 'backtrace'=>$back_trace));
		}
	}

	function show() {
		$content = output()->get(CForm('lib/system/log.tpl')->parse(array(
			'items' => $this->table->select_all(),
			'nl' => "\n",
			'running_time' => microtime(true) - $this->start_time
		)));
		$file_name = ROOT_DIR.'publish/log.txt';
		//$file_name = ROOT_DIR.'publish/log.html';
		file_put_contents($file_name,'<h1>Request:</h1>'.var_export($_POST,1).'<h2>Log:</h2>'.
			$content.((file_exists($file_name) and filesize($file_name)<500000)?
				file_get_contents($file_name):'<script src="3rdparty/jQuery/jquery-1.2.6.js"></script>'));
	}
}

function &CLog($type = '', $message = '')
{
	if(!CLog::$current)
	{
		CLog::$current = new CLog(new CTable(CStore()));
	}
	if($type and $message)
	{
		CLog::$current->log($type, $message);
	}
	return CLog::$current;
}
?>